package android.slc.appbase.ui.vd;

import android.slc.appbase.api.slc.callback.SlcObserver;
import android.slc.appbase.ui.vd.wrapper.BaseListWrapper;
import android.slc.appbase.ui.vd.wrapper.IBaseListWrapper;
import android.slc.utils.entity.ValueWrap;
import javafx.beans.property.ReadOnlyObjectProperty;

import java.io.Serializable;
import java.util.List;

/**
 * 列表数据基础类
 *
 * @param <T>
 */
public abstract class BaseListVmBox<T extends Serializable> extends BaseListCommonVmBox<T> implements IBaseListWrapper<T> {

    private final BaseListWrapper<T> listWrapper;

    public BaseListVmBox() {
        this.listWrapper =createListWrapper();
    }

    protected BaseListWrapper<T> getListWrapper(){
        return listWrapper;
    }

    protected BaseListWrapper<T> createListWrapper() {
        return new BaseListWrapper<T>(this);
    }

    @Override
    public void refresh() {
        this.getListWrapper().startRefreshOf.notifyChange();
    }

    /**
     * 相应成功
     *
     * @param data
     */
    public void onSucceed(List<T> data) {
        dataListOf.get().getData().clear();
        dataListOf.get().getData().addAll(data);
        dataListOf.notifyChange();
    }

    @Override
    protected final void onFailed(int errorCode, String errorMessage) {
        super.onFailed(errorCode, errorMessage);
    }

    @Override
    public void onError(int errorCode, String errorMessage) {
        onFailed(errorCode, errorMessage);
    }

    @Override
    public ReadOnlyObjectProperty<ValueWrap<Void>> getStartRefreshOf() {
        return this.getListWrapper().getStartRefreshOf();
    }

    @Override
    public SlcObserver<List<T>> getDataListSlcObserver() {
        return this.getListWrapper().getDataListSlcObserver();
    }
}
